import { getUserChannerls, getChannerls } from '@/api/channels'

const state = {
  channelList: [], // 我的频道
  channelListAll: [] // 可选频道
}
const mutations = {
  setChannelList(state, payload) {
    state.channelList = payload
  },

  setAllChannelList(state, payload) {
    state.channelListAll = payload
  },

  addChhanneList(state, payload) {
    state.channelList.push(payload)
  },

  // 删除我的频道
  delChannel(state, obj) {
    state.channelList = state.channelList.filter(item => item !== obj)
  }
}
const actions = {

  // 我的频道请求
  async actions_getChannelList(context) {
    const res = await getUserChannerls()
    console.log(res)
    context.commit('setChannelList', res.data.channels)
  },

  // 全部频道请求
  async actions_getAllChannelList(context) {
    const res = await getChannerls()
    context.commit('setAllChannelList', res.data.channels)
    console.log(res.data.channels)
  }
}

const getters = {
  // 使用计算属性，将全部频道过滤，过滤条件为id不等于我的频道剩下的就是可选频道
  optionsChannelList(state) {
    return state.channelListAll.filter(item => {
      return !state.channelList.some(obj => obj.id === item.id)
    })
  }
}

export default {
  state,
  mutations,
  getters,
  actions,
  namespaced: true
}
